package itsm.isperp.workflow.listener;

import itsm.isperp.workflow.utils.BpmScriptUtils;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 节点事件监听器
 * 
 * @author lizx
 * @date 2013-4-25
 */
public abstract class BaseNodeEventListener implements ExecutionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public void notify(DelegateExecution execution) throws Exception {
		logger.debug((new StringBuilder())
				.append("enter the node event listener..")
				.append(execution.getId()).toString());
		ExecutionEntity ent = (ExecutionEntity) execution;
		if (ent.getActivityId() == null) {
			return;
		} else {
			String actDefId = ent.getProcessDefinitionId();
			String nodeId = ent.getActivityId();
			execute(ent, actDefId, nodeId);

			try {
				String s = getScriptType();
				BpmScriptUtils.exeEventScript(execution, execution, s,
						actDefId, nodeId);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);// 记录异常的发生;
			}
		}
	}

	protected abstract void execute(ExecutionEntity delegateexecution,
			String defId, String nodeId);

	protected abstract String getScriptType();

}
